// import api from '../urls'
// import { post } from '../request'

export function requestShareId(inrouteId, params = {}) {
  return new Promise((resolve, reject) => {
    resolve(-1)
    // const queryString = inrouteId ? `?inrouteId=${inrouteId}` : '?'
    // const extQs = Object.keys(params).map(key => `${key}=${params[key]}`).join('&')
    // post(`${api.getShareIdUrl}${queryString}${extQs}`, {}, { silentFail: true })
    //   .then(res => resolve(res.data.data))
    //   .catch(error => reject(error))
  })
}

let fromShareId = null
let outShareId = null
// 更新shareId
function refreshShareId(fromShareId) {
  requestShareId(fromShareId).then(id => {
    outShareId = id
  }).catch(() => {
    setTimeout(() => refreshShareId(fromShareId), 5000)
  })
}

// 设置来源shareId 并立即换取新的shareId
export function setFromShareId(shareId) {
  fromShareId = shareId || null
  refreshShareId(fromShareId)
}

// 消费同时更新shareId
export default function getShareId() {
  refreshShareId(fromShareId)
  return outShareId || ''
}
